package it.slkfarm.android;

import it.slkfarm.log.LogData;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

public class ChooseLanguage extends Base implements OnClickListener
{
	/**
	 * @uml.property  name="en"
	 * @uml.associationEnd  
	 */
	private ImageButton en;
	/**
	 * @uml.property  name="sri"
	 * @uml.associationEnd  
	 */
	private ImageButton sri;
	/**
	 * @uml.property  name="title"
	 * @uml.associationEnd  
	 */
	private TextView title;
	/**
	 * @uml.property  name="change"
	 */
	private boolean change;
	
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.custom_dialog_language);
		startService();
		getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/KMKDSP__.ttf");
		title = (TextView) findViewById(R.id.tvlangTitleUk);
		title.setTypeface(tf);
		en = (ImageButton) findViewById(R.id.btnUk);
		sri = (ImageButton) findViewById(R.id.btnSri);
		en.setOnClickListener(this);
		sri.setOnClickListener(this);
		change = getIntent().getBooleanExtra(getPackageName() + ".change", false);
		if (!change)
		{
			Log.i("choose", "get from db");
			int lang = dbAdapter.getSetLanguage();
			if (lang != NO_LANG)
				start(lang);
		}
    }

	public void onClick(View v) 
	{
		if (v.getId() == en.getId())
		{
			if (!change)
			{
				LogData log = new LogData("ChooseLanguage","Button UK","UK language select");
				logger.appendLog(log);
				dbAdapter.setLanguage(EN_LANG);
				log = new LogData("ChooseLanguage","DB","UK language saved");
				logger.appendLog(log);
			}
				
			else
			{
				LogData log = new LogData("ChooseLanguage","Button SLK","SLK language select");
				logger.appendLog(log);
				dbAdapter.changeLanguage(EN_LANG, dbAdapter.getSetLanguage());
				log = new LogData("ChooseLanguage","DB","SLK language saved");
				logger.appendLog(log);
			}	
			start(EN_LANG);
		}
		else
		{
			if (!change)
				dbAdapter.setLanguage(SRI_LANG);
			else
				dbAdapter.changeLanguage(SRI_LANG, dbAdapter.getSetLanguage());
			start(SRI_LANG);
		}
	}
	
	public void start(int lang)
	{
		Log.i("start choose", String.valueOf(lang));
		Intent i = new Intent(this, Login.class);
		finish();
		setLanguage(lang);
		startActivity(i);	
	}
}
